import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { assetLampPostState } from './typings'
import {
  getLampPostSelectBoxListByKey,
  getAssetLampPostList,
  getAssetLampPostInfo,
  addAssetLampPostInfo,
  updateAssetLampPostInfo,
  checkDeleteAssetLampPostInfo,
  deleteAssetLampPostInfo,
  exportAssetLampPostInfo,
  getDeviceModelByLampPostType,
  exportAssetLampPostExcelTemplate,
  importAssetLampPostExcel,
  importAssetLampPostUpdateExcel,
  updateAssetLampPostMapLocation
  // getLampPostTypeSelectList
} from '~/api/assetManagement/facilityManagement/assetLampPost'
// import { getNodeByParentCode } from '~/api/assetManagement/facilityManagement/powerDistributionFacilities'
import { getTenantDictDataDictionaryByCode } from '~/api/global'
import { filterValueEnum, DownloadAttachment } from '~/utils/util'

const assetLampPost: Module<assetLampPostState, assetLampPostState> = {
  namespaced: true,
  state: {
    suborinateLampPostSelectList: [], // 查询所属灯杆 下拉列表
    suborinateLampPostEnum: {}, // 查询所属灯杆 枚举
    lampPostTypeSelectList: [], // 灯杆类型 下拉列表
    lampPostTypeEnum: {}, // 灯杆类型 枚举
    deviceModelBylampPostTypeSelectList: [], // 根据灯杆类型 查灯杆规格型号 下拉列表
    deviceModelBylampPostTypeEnum: {} // 根据灯杆类型 查灯杆规格型号 枚举
  },
  getters: {
    suborinateLampPostSelectList: (state) => state.suborinateLampPostSelectList,
    suborinateLampPostEnum: (state) => state.suborinateLampPostEnum,
    lampPostTypeSelectList: (state) => state.lampPostTypeSelectList,
    lampPostTypeEnum: (state) => state.lampPostTypeEnum,
    deviceModelBylampPostTypeSelectList: (state) => state.deviceModelBylampPostTypeSelectList,
    deviceModelBylampPostTypeEnum: (state) => state.deviceModelBylampPostTypeEnum
  },
  mutations: {
    SET_SUBORDINATE_LAMPPOST_SELECT_LIST: (state, payload) => {
      state.suborinateLampPostSelectList = payload
      state.suborinateLampPostEnum = filterValueEnum(payload, 'id', 'name')
    },
    SET_LAMPPOST_TYPE_SELECT_LIST: (state, payload) => {
      state.lampPostTypeSelectList = payload
      state.lampPostTypeEnum = filterValueEnum(payload, 'id', 'dictValue')
    },
    SET_DEVICEMODEL_SELECT_LIST: (state, payload) => {
      state.deviceModelBylampPostTypeSelectList = payload
      state.deviceModelBylampPostTypeEnum = filterValueEnum(payload, 'id', 'name')
    }
  },
  actions: {
    // 获取灯杆主列表
    async GetAssetLampPostList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getAssetLampPostList<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 新增灯杆 基础资料
    async AddAssetLampPostInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await addAssetLampPostInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 编辑灯杆 基础资料 原内容
    async GetAssetLampPostInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await getAssetLampPostInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 编辑灯杆 基础资料 提交
    async UpdateAssetLampPostInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await updateAssetLampPostInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 编辑灯具 地图位置
    async UpdateAssetLampPostMapLocation<S, T>(_: ActionContext<S, T>, params: T) {
      return await updateAssetLampPostMapLocation<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 校验灯杆是否可删除
    async CheckDeleteAssetLampPostInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await checkDeleteAssetLampPostInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 删除灯杆
    async DeleteAssetLampPostInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await deleteAssetLampPostInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 灯杆类型 下拉列表
    async GetNodeByParentCode<S, T>({ commit }: ActionContext<S, T>) {
      return await getTenantDictDataDictionaryByCode({ code: 'lamp_post_cate' }).then(
        (res: AxiosResponse) => {
          commit('SET_LAMPPOST_TYPE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },

    // 所属灯杆
    async GetLampPostSelectBoxListByKey<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getLampPostSelectBoxListByKey<T>(params).then((res: AxiosResponse) => {
        commit('SET_SUBORDINATE_LAMPPOST_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },

    // 根据灯杆类型 查灯杆规格型号
    async GetDeviceModelByLampPostType<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getDeviceModelByLampPostType<T>(params).then((res: AxiosResponse) => {
        commit('SET_DEVICEMODEL_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },

    // 导出灯杆
    async ExportAssetLampPostInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await exportAssetLampPostInfo<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },

    // 导出灯杆表格模板
    async ExportAssetLampPostExcelTemplate<S, T>(_: ActionContext<S, T>, params: T) {
      return await exportAssetLampPostExcelTemplate<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },

    // 导入灯杆
    async ImportAssetLampPostExcel<S, T>(_: ActionContext<S, T>, params: T) {
      return await importAssetLampPostExcel<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 批量更新
    async ImportAssetLampPostUpdateExcel<S, T>(_: ActionContext<S, T>, params: T) {
      return await importAssetLampPostUpdateExcel<T>(params).then((res: AxiosResponse) => res?.data)
    }
  }
}

export default assetLampPost
